<?php 
/***************************************************
* SimpleUpload Component
*
* Manages uploaded files to be saved to the file system.
*
* @copyright    Copyright 2009, MAZE studio Armenia
* @link         http://www.mazestudio.com
* @author       Arman Mkrtchyan
* @version      1.0
* @license      MAZE
*/
class SimpleUploadComponent extends Object{
  
	
	/** 
	 * Uploads files to the server
	 * 
	 * @param $in_sFolder		The folder to upload the files e.g. 'img/files'
	 * @param $in_oFormData		The array containing the form files
	 * @param $in_iItemId		The id of the item (optional) will create a new sub folder
	 * @param $in_oPermitted	The array containing the permitted roles
	 * 
	 * @return array with the success of each file upload
	 */
    public function uploadFiles(
    	$in_sFolder,
    	$in_oFormData,
    	$in_iItemId = null,
    	$in_oPermitted = array( 'image/gif', 'image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png' ) )
    {
    	//---------------------------------------
    	// Setup dir names absolute and relative.
    	//--------------------------------------- 
    	$sFolderUrl		= WWW_ROOT . $in_sFolder;
    	$sRelUrl		= $in_sFolder;
    	$out_oResult	= array();
    	
    	//----------------------------------------
    	// Create the folder if it does not exist.
    	//----------------------------------------
    	if( !is_dir( $sFolderUrl ) )
    	{
    		mkdir( $sFolderUrl );
    	}
    	
    	//----------------------------------------
    	// If itemId is set create an item folder.
    	//----------------------------------------
    	if( $in_iItemId )
    	{
    		//-------------------------
    		// Set new absolute folder.
    		//------------------------- 
    		$sFolderUrl	= WWW_ROOT . $in_sFolder . '/' . $in_iItemId;
    		
    		//-------------------------
    		// Set new relative folder.
    		//------------------------- 
    		$sRelUrl	= $in_sFolder . '/' . $in_iItemId;

    		//------------------
    		// Create Directory.
    		//------------------
    		if( !is_dir( $sFolderUrl ) )
    		{
    			mkdir( $sFolderUrl );
    		}
    	}
    	
    	//--------------------------------------
    	// Loop through and deal with the files.
		//--------------------------------------
		foreach( $in_oFormData as $oFile )
		{
			//---------------------------------
			// Replace spaces with underscores.
			//---------------------------------
			$sFilename	= str_replace( ' ', '_', $oFile[ 'name' ] );
			
			//--------------------------
			// Assume filetype is false.
			//--------------------------
			$isTypeOK	= false;
			
			//----------------------
			// Check filetype is ok.
			//----------------------
			foreach( $in_oPermitted as $oType )
			{
				if( $oType == $oFile[ 'type' ] )
				{
					$isTypeOK	= true;
					break;
				}
			}
			
			//---------------------------------
			// If file type ok upload the file.
			//---------------------------------
			if( $isTypeOK )
			{
				//----------------------------
				// Switch based on error code.
				//----------------------------
				switch( $oFile[ 'error' ] )
				{
					case 0:
						//-------------------------------
						// Check filename already exists.
						//-------------------------------
						if( !file_exists( $sFolderUrl . '/' . $sFilename ) )
						{
							//----------------------
							// Create full filename.
							//----------------------
							$sFullUrl	= $sFolderUrl . '/' . $sFilename;
							$sUrl		= $sRelUrl . '/' . $sFilename;
							
							//-----------------
							// Upload the file.
							//-----------------
							$isSuccess	= move_uploaded_file( $oFile[ 'tmp_name' ], $sFullUrl );
						}
						else
						{
							// create unique filename and upload file  
							ini_set( 'date.timezone', 'Europe/London' );
							$sNow		= date( 'Y-m-d-His' );
							$sFullUrl	= $sFolderUrl . '/' . $sNow . $sFilename;
							$sUrl		= $sRelUrl . '/' . $sNow . $sFilename;  
							$isSuccess	= move_uploaded_file( $oFile[ 'tmp_name' ], $sFullUrl );
						}
						
						//--------------------------
						// If upload was successful.
						//--------------------------
						if( $isSuccess )
						{
							//--------------------------
							// Save the url of the file.
							//--------------------------
							$out_oResult[ 'urls' ][]	= $sUrl;
						}
						else
						{
							$out_oResult[ 'errors' ][]	= 'Error uploading ' .
								$sFilename . '. Please try again.';
						}
						break;
					
					case 3:
						//------------------
						// An error occured.
						//------------------  
						$out_oResult[ 'errors' ][]	= 'Error uploading ' .
							$sFilename . '. Please try again.'; 
						break;
					
					default:
						//------------------
						// An error occured.
						//------------------
						$out_oResult[ 'errors' ][]	= 'System error uploading ' .
							$sFilename . '. Contact webmaster.';
						break;
				}
			}
			else if( $oFile['error'] == 4 )
			{
				//---------------------------------
				// No file was selected for upload.
				//---------------------------------
				$out_oResult[ 'nofiles' ][]	= 'No file Selected';
			}
			else
			{
				//------------------------
				// Unacceptable file type.
				//------------------------
				$out_oResult[ 'errors' ][]	= $sFilename .
					' cannot be uploaded. Acceptable file types: gif, jpg, png.';
			}
		}
		
		return $out_oResult;
    } 

	
	
}
?>